<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { preservationStatistics, preservationCount } from '../../../api/modules/home'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      chart: null,
      sidebarElm: null,
      chartData: {
        visitsData: [],
        ipData: [],
        arrThree: [],
        arrFour: [],
        arrFive: [],
        arrSix: [],
        arrSeven: [],
        arrEight: [],
        arrNine: [],
        arrTen: []
      },
      weekDays: [],
      title: []
    }
  },
  created () {
    // this.preservationStatistics()
  },
  mounted () {
    preservationStatistics().then((res) => {
      let obj = res.obj.number
      let number = []
      let title = []
      for (let key in obj) {
        number.push(obj[key])
        title.push(key)
      }
      this.chartData.visitsData = number[0]
      this.chartData.ipData = number[1]
      this.chartData.arrThree = number[2]
      this.chartData.arrFour = number[3]
      this.chartData.arrFive = number[4]
      this.chartData.arrSix = number[5]
      this.chartData.arrSeven = number[6]
      this.chartData.arrEight = number[7]
      this.chartData.arrNine = number[8]
      this.chartData.arrTen = number[9]
      let dates = res.obj.dates.日期
      let newDates = []
      for (let i = 0; i < dates.length; i++) {
        let newTime = this.format(dates[i])
        newDates.push(newTime)
      }
      this.weekDays = newDates
      this.title = title
      this.initChart()

    })
    // getChartData().then(res => {
    //   this.chartData.visitsData = res.visitsData
    //   this.chartData.ipData = res.ipData
    //   this.weekDays = res.weekDays
    // this.initChart()
    // this.preservationStatistics()
    // })
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }

    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    sidebarResizeHandler (e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    },
    setOptions ({ visitsData, ipData, arrThree, arrFour, arrFive, arrSix, arrSeven, arrEight, arrNine, arrTen } = {}) {
      this.chart.setOption({
        title: {
          text: '近一周存证平台数据图'
        },
        xAxis: {
          data: this.weekDays,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 30,
          right: 30,
          bottom: 20,
          top: 50,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: this.title
        },
        series: [{
          name: this.title[0],
          itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: visitsData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: this.title[1],
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: ipData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }, {
          name: this.title[2],
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#ff8000',
              lineStyle: {
                color: '#ff8000',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: arrThree,
          animationDuration: 2800,
          animationEasing: 'quarticOut'
        },
        {
          name: this.title[3],
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#ff0000',
              lineStyle: {
                color: '#ff0000',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: arrFour,
          animationDuration: 2800,
          animationEasing: 'quarticOut'
        },
        {
          name: this.title[4],
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#00ff40',
              lineStyle: {
                color: '#00ff40',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: arrFive,
          animationDuration: 2800,
          animationEasing: 'quarticOut'
        },
        {
          name: this.title[5],
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#ff00bf',
              lineStyle: {
                color: '#ff00bf',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: arrSix,
          animationDuration: 2800,
          animationEasing: 'quarticOut'
        },
        {
          name: this.title[6],
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#ffff00',
              lineStyle: {
                color: '#ffff00',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: arrSeven,
          animationDuration: 2800,
          animationEasing: 'quarticOut'
        },
        {
          name: this.title[7],
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#00ffff',
              lineStyle: {
                color: '#00ffff',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: arrEight,
          animationDuration: 2800,
          animationEasing: 'quarticOut'
        },
        {
          name: this.title[8],
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#808080',
              lineStyle: {
                color: '#808080',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: arrNine,
          animationDuration: 2800,
          animationEasing: 'quarticOut'
        },
        {
          name: this.title[9],
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#000000',
              lineStyle: {
                color: '#000000',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: arrTen,
          animationDuration: 2800,
          animationEasing: 'quarticOut'
        }]
      })
    },
    initChart () {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      // this.preservationStatistics()
    },
    format (time) {
      let newTime = new Date(time)
      let timeValue = newTime.toLocaleDateString().replace(/\//g, "-")
      return timeValue
    }
  }
}
</script>
